package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.sql.SqlError;
import com.lineage.server.templates.L1MapTile;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MapTileTable {
  private static final Log _log = LogFactory.getLog(MapTileTable.class);
  
  private static final List<L1MapTile> _list = new ArrayList<>();
  
  public static MapTileTable get() {
    return Holder.instance;
  }
  
  private MapTileTable() {
    load();
  }
  
  private void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `mapids_tile`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int mapId = rs.getInt("map_id");
        int x = rs.getInt("x");
        int y = rs.getInt("y");
        int tile = rs.getInt("tile");
        L1MapTile mapTile = new L1MapTile();
        mapTile.setMapid(mapId);
        mapTile.setX(x);
        mapTile.setY(y);
        mapTile.setTile(tile);
        _list.add(mapTile);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public List<L1MapTile> getList() {
    return _list;
  }
  
  public void addData(int mapId, int x, int y, int tile) {
    Connection co = null;
    PreparedStatement ps = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement("INSERT INTO `mapids_tile` SET `map_id`=?,`x`=?,`y`=?,`tile`=?");
      int i = 0;
      ps.setInt(++i, mapId);
      ps.setInt(++i, x);
      ps.setInt(++i, y);
      ps.setLong(++i, tile);
      ps.execute();
    } catch (Exception e) {
      SqlError.isError(_log, e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
  }
  
  private static class Holder {
    static MapTileTable instance = new MapTileTable();
  }
}
